package com.ss.android.article.base.feature.update.b;

import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Pair;

import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.UIUtils;
import com.bytedance.common.utility.collection.WeakHandler;
import com.bytedance.common.utility.collection.WeakHandler.Callback;
import com.bytedance.common.utility.collection.WeakList;
import com.ss.android.account.Spipe;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.app.a.ArticleConstants;
import com.ss.android.article.base.feature.app.b.DBHelper;
import com.ss.android.article.base.feature.message.n;
import com.ss.android.article.base.feature.update.a.e;
import com.ss.android.article.browser.R;
import com.ss.android.common.AbsApiThread;
import com.ss.android.common.lib.MobClickCombiner;
import com.ss.android.common.load.AsyncLoader;
import com.ss.android.common.dialog.AlertDialog;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.common.util.UrlBuilder;
import com.ss.android.model.ItemActionV3;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public final class UpdateItemMgr implements Callback {
    public interface o$a {
        void a(long arg1);

        void a(long arg1, e arg2);

        void b(long arg1);
    }

    public interface b {
        void a();

        void c(long arg1);
    }

    final Context mContext;
    public final Spipe mSpipe;
    public long c;
    public WeakList d;
    public final WeakList e;
    public e f;
    public com.bytedance.common.utility.collection.e g;
    public AsyncLoader h;
    public AsyncLoader i;
    Handler j;
    private static UpdateItemMgr sInstance;
    private WeakList l;
    private AsyncLoader.LoaderProxy m;
    private AsyncLoader.LoaderProxy n;
    private static n o;
    private static n p;
    private static n q;
    private static n r;

    private UpdateItemMgr(Context context) {
        this.c = 0;
        this.d = new WeakList();
        this.l = new WeakList();
        this.e = new WeakList();
        this.f = null;
        this.g = new com.bytedance.common.utility.collection.e();
        this.m = new p(this);
        this.h = new AsyncLoader(3, 1, this.m);
        this.n = new q(this);
        this.i = new AsyncLoader(5, 1, this.n);
        this.mContext = context.getApplicationContext();
        this.mSpipe = Spipe.inst();
        if (this.mSpipe.isLogin) {
            this.c = this.mSpipe.userId;
        }

        this.j = new WeakHandler(Looper.getMainLooper(), ((Callback) this));
    }

    public static UpdateItemMgr getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new UpdateItemMgr(context);
        }

        return sInstance;
    }

    public static void showDeleteConfirmDialog(Context context, DialogInterface.OnClickListener positiveClickListener) {
        if (context != null) {
            AppData.getInstance();
            AlertDialog.Builder builder = com.ss.android.j.b.createDialogBuilder(context);
            builder.setMessage(com.ss.android.article.browser.R.string.tip_delete_update);
            builder.setNegativeButton(com.ss.android.article.browser.R.string.label_cancel, null);
            builder.setPositiveButton(com.ss.android.article.browser.R.string.label_ok, positiveClickListener);
            builder.show();
        }
    }

    public static n a(Context context, String arg4) {
        n v0;
        if ("comment".equals(arg4)) {
            if (o == null) {
                o = new n(context.getApplicationContext(), "comment");
            }

            v0 = o;
        } else {
            if ("digg".equals(arg4)) {
                if (p == null) {
                    p = new n(context.getApplicationContext(), "digg");
                }

                return p;
            }

            if (q == null) {
                q = new n(context.getApplicationContext(), "");
            }

            v0 = q;
        }

        return v0;
    }

    public final e a(long arg4) {
        com.ss.android.article.base.feature.update.a.d v1 = this.b(arg4);
        e v0 = v1 == null ? null : new e(((com.ss.android.article.base.feature.update.a.a) v1));
        return v0;
    }

    static com.ss.android.article.base.feature.update.a.d a(long arg8, com.ss.android.article.base.feature.update.a.d arg10, int arg11) {
        com.ss.android.article.base.feature.update.a.d v0_2 = null;
        JSONObject v2_1;
        com.ss.android.article.base.feature.update.a.d v1 = null;
        try {
            UrlBuilder urlBuilder = new UrlBuilder(ArticleConstants.dongtaiDetailUrlV7);
            String v0_1 = "id";
            if (arg11 == 5) {
                v0_1 = "comment_id";
            }

            urlBuilder.addParam(v0_1, arg8);
            if (arg10 != null) {
                urlBuilder.addParam("modify_time", arg10.modifyTime);
            }

            if (arg11 >= 0) {
                urlBuilder.addParam("source", arg11);
            }

            v0_1 = NetworkUtils.doGet(-1, urlBuilder.build());
            if (android.support.design.a.isTextEmpty(v0_1)) {
                return v1;
            }

            v2_1 = new JSONObject(v0_1);
            if (!AbsApiThread.isApiSuccess(v2_1)) {
                return v1;
            }

            v2_1 = v2_1.optJSONObject("data");
            if (v2_1 != null && v2_1.length() > 0) {
                v0_2 = new com.ss.android.article.base.feature.update.a.d(v2_1.optLong("id"));
            } else {
                return v1;
            }
            v0_2.a(v2_1);
            v0_2.r = System.currentTimeMillis();
            return v0_2;
        } catch (Exception v1_1) {
            Logger.w("UpdateItemMgr", "refresh update detail exception: " + v1_1);
            return v0_2;
        }
    }

    private List a(List arg7) {
        ArrayList v0;
        this.g.b();
        ArrayList v3 = new ArrayList();
        if (arg7.isEmpty()) {
            v0 = v3;
        } else {
            com.bytedance.common.utility.collection.e v4 = this.g;
            Iterator v5 = arg7.iterator();
            while (v5.hasNext()) {
                Object v0_1 = v5.next();
                String v2 = ((e) v0_1).a.getItemKey();
                Object v1 = v4.a(v2);
                if (v1 == null) {
                    v4.a(v2, ((e) v0_1).a);
                    ((List) v3).add(v0_1);
                } else {
                    ((com.ss.android.article.base.feature.update.a.d) v1).a((com.ss.android.article.base.feature.update.a.d) ((e) v0_1).a);
                    ((List) v3).add(new e(((com.ss.android.article.base.feature.update.a.a) v1)));
                }

                com.ss.android.article.base.feature.update.a.d v1_1 = ((com.ss.android.article.base.feature.update.a.d) ((e) v0_1).a).e;
                if (v1_1 == null) {
                    continue;
                }

                v2 = v1_1.getItemKey();
                v0_1 = v4.a(v2);
                if (v0_1 == null) {
                    v4.a(v2, v1_1);
                    continue;
                }

                ((com.ss.android.article.base.feature.update.a.d) v0_1).a(v1_1);
            }

            v0 = v3;
        }

        return ((List) v0);
    }

    public final e a(com.ss.android.article.base.feature.update.a.d arg4) {
        e v0;
        if (arg4 == null) {
            v0 = null;
        } else {
            ArrayList v1 = new ArrayList();
            v0 = new e(arg4);
            ((List) v1).add(v0);
            List v1_1 = this.a(((List) v1));
            if (!v1_1.isEmpty()) {
                v0 = (com.ss.android.article.base.feature.update.a.e) v1_1.get(0);
            }
        }

        return ((e) v0);
    }

    final void a(com.ss.android.article.base.feature.update.a.d arg7, long arg8) {
        int v0;
        if (arg8 > 0 && arg7 != null) {
            Iterator<com.ss.android.article.base.feature.update.a.c> v2 = arg7.p.f.iterator();
            int v1;
            for (v1 = 0; v2.hasNext(); v1 = v0) {
                if (v2.next().a == arg8) {
                    v2.remove();
                    v0 = 1;
                } else {
                    v0 = v1;
                }
            }

            if (v1 == 0) {
                return;
            }

            --arg7.c;
            this.b(arg7);
            Iterator v1_1 = this.d.iterator();
            while (v1_1.hasNext()) {
                Object v0_1 = v1_1.next();
                if (v0_1 == null) {
                    continue;
                }

                ((o$a) v0_1).a(arg7.i);
                ((o$a) v0_1).b(arg8);
            }
        }
    }

    public static n b(Context arg2) {
        if (r == null) {
            r = new n(arg2.getApplicationContext());
        }

        return r;
    }

    public final com.ss.android.article.base.feature.update.a.d b(long arg4) {
        com.ss.android.article.base.feature.update.a.d v0;
        if (arg4 <= 0) {
            v0 = null;
        } else {
            v0 = (com.ss.android.article.base.feature.update.a.d) this.g.a(String.valueOf(arg4));
        }

        return v0;
    }

    public final void b(com.ss.android.article.base.feature.update.a.d arg4) {
        if (arg4 != null) {
            DBHelper v0 = DBHelper.getInstance(this.mContext);
            Message v1 = this.j.obtainMessage(10);
            if (arg4 != null && v1 != null) {
                v1.obj = arg4;
                v0.b(109, v1);
            }
        }
    }

    public final void c(long arg4) {
        if (arg4 > 0) {
            Iterator v0 = this.l.iterator();
            while (v0.hasNext()) {
                v0.next();
            }
        }
    }

    final void d(long arg4) {
        if (arg4 > 0) {
            com.ss.android.article.base.feature.update.a.d v0 = this.b(arg4);
            if (v0 != null) {
                v0.d = true;
                this.b(v0);
            }

            Iterator v1 = this.e.iterator();
            while (v1.hasNext()) {
                Object v0_1 = v1.next();
                if (v0_1 == null) {
                    continue;
                }

                ((b) v0_1).c(arg4);
            }
        }
    }

    public final void e(long arg6) {
        if (arg6 > 0 && !this.e.weakHashMap.isEmpty()) {
            HashSet v1 = new HashSet();
            Iterator v2 = this.e.iterator();
            while (v2.hasNext()) {
                Object v0 = v2.next();
                if (v0 == null) {
                    continue;
                }
                ((b) v0).a();
            }

            if (!((Set) v1).isEmpty()) {
                Iterator<Long> v1_1 = ((Set) v1).iterator();
                while (v1_1.hasNext()) {
                    com.ss.android.article.base.feature.update.a.d v0_1 = this.b(v1_1.next().longValue());
                    if (v0_1 == null) {
                        continue;
                    }

                    v0_1.d = true;
                    this.b(v0_1);
                }
            }
        }
    }

    public final boolean checkLoginAndNet(long userId) {
        int v4 = R.drawable.close_popup_textpage;
        boolean v0 = false;
        if (userId > 0) {
            if (!this.mSpipe.isLogin) {
                UIUtils.showImageToast(this.mContext, v4, R.string.ss_hint_not_login);
            } else if (this.mSpipe.userId == userId) {
                if (!NetworkUtils.isNetworkAvailable(this.mContext)) {
                    UIUtils.showImageToast(this.mContext, v4, R.string.jp);
                } else {
                    v0 = true;
                }
            }
        }

        return v0;
    }

    public final void deleteComment(long commentId) {
        if (commentId > 0) {
            MobClickCombiner.onEvent(this.mContext, "comment", "delete_confirm");
            new DeleteCommentThread(this.mContext, this.j, commentId, 4).start();
        }
    }

    public final void handleMsg(Message message) {
        Object v0_1;
        long v2;
        long v6 = 0;
        switch (message.what) {
            case 10: {
                if ((message.obj instanceof com.ss.android.article.base.feature.update.a.d)) {
                    DBHelper v1_1 = DBHelper.getInstance(this.mContext);
                    v2 = this.c;
                    v0_1 = message.obj;
                    if (v0_1 == null) {
                        return;
                    }

                    try {
                        String v4_2 = ((com.ss.android.article.base.feature.update.a.d) v0_1).a().toString();
                        ContentValues v5 = new ContentValues();
                        v5.put("ss_op_key", Integer.valueOf(103));
                        v5.put("user_id", Long.valueOf(v2));
                        v5.put("update_id", Long.valueOf(((com.ss.android.article.base.feature.update.a.d) v0_1).i));
                        v5.put("refresh_time", Long.valueOf(((com.ss.android.article.base.feature.update.a.d) v0_1).r));
                        v5.put("item_json", v4_2);
                        v1_1.asyncUpdate(v5);
                        com.ss.android.article.base.feature.update.a.d v0_4 = ((com.ss.android.article.base.feature.update.a.d) v0_1).e;
                        if (v0_4 == null) {
                            return;
                        }

                        v4_2 = v0_4.a().toString();
                        v5 = new ContentValues();
                        v5.put("ss_op_key", Integer.valueOf(103));
                        v5.put("user_id", Long.valueOf(v2));
                        v5.put("update_id", Long.valueOf(v0_4.i));
                        v5.put("refresh_time", Long.valueOf(v0_4.r));
                        v5.put("item_json", v4_2);
                        v1_1.asyncUpdate(v5);
                    } catch (Exception v0_3) {
                    }
                }
                return;
            }
            case 10001: {
                if ((message.obj instanceof DeleteCommentThread.c$a)) {
                    Object v4 = message.obj;
                    if (v4 == null) {
                        return;
                    }

                    if (((DeleteCommentThread.c$a) v4).a <= v6) {
                        return;
                    }

                    if (((DeleteCommentThread.c$a) v4).b != 5 && ((DeleteCommentThread.c$a) v4).b != 6) {
                        return;
                    }

                    if (((DeleteCommentThread.c$a) v4).c > 0) {
                        ItemActionV3 v0 = new ItemActionV3("delete",
                                ((DeleteCommentThread.c$a) v4).a,
                                ((DeleteCommentThread.c$a) v4).b, System.currentTimeMillis());
                        com.ss.android.action.bb.a();
                        com.ss.android.action.bb.a(v0);
                        return;
                    }

                    Iterator<Long> v1 = ((DeleteCommentThread.c$a) v4).d.iterator();
                    while (v1.hasNext()) {
                        v2 = v1.next().longValue();
                        if (v2 <= v6) {
                            continue;
                        }

                        this.d(v2);
                    }

                    Iterator<Pair<Long, Long>> v2_1 = ((DeleteCommentThread.c$a) v4).e.iterator();
                    while (true) {
                        if (!v2_1.hasNext()) {
                            return;
                        }

                        Pair<Long, Long> pair = v2_1.next();
                        long v4_1 = pair.first.longValue();
                        long v0_2 = pair.second.longValue();
                        if (v4_1 <= v6) {
                            continue;
                        }

                        if (v0_2 <= v6) {
                            continue;
                        }

                        com.ss.android.article.base.feature.update.a.d v3 = this.b(v4_1);
                        if (v3 == null) {
                            continue;
                        }

                        this.a(v3, v0_2);
                    }
                }
            }

            return;


        }
    }
}

